//
//  YygFirstViewController.m
//  ShangChengMoBan
//
//  Created by 军 on 16/8/26.
//  Copyright © 2016年 ping. All rights reserved.
//一元购首页面

#import "YygFirstViewController.h"
#import "SDCycleScrollView.h"
#import "YygCollectionViewCell.h"
#import "YygDetailsViewController.h"
#import "YYG_FirstModel.h"
#import "YYG_AllFirstModel.h"
@interface YygFirstViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,SDCycleScrollViewDelegate>
@end
@implementation YygFirstViewController
{
    YYG_AllFirstModel * _allFirst;
    RWcollectionView * _collectionView;
    NSMutableArray *_allArr;
    NSMutableArray * _imageArr;
    int _page;
    SDCycleScrollView * _sdcView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self drawNavWithTitle:@"FTP一元购" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    [self drawBackButton];
       [self headerSDC];
     [self requestDataWith:1];
    [TCUtils showLoadingWithView:self.view];
}
-(void)headerSDC{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 173)];
    view .backgroundColor =[UIColor colorWithHue:0.00 saturation:0.00 brightness:0.91 alpha:1.00];
    _sdcView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KScreenWidth, 130) imagesGroup:nil];
   _sdcView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
   _sdcView.autoScrollTimeInterval = 3;
    _sdcView.delegate = self; 
   _sdcView.dotColor = [UIColor whiteColor];
   _sdcView.placeholderImage = [UIImage imageNamed:PLaceHolderImage];
    [view addSubview:_sdcView];
    UIView * viewTitle = [[UIView alloc]initWithFrame:CGRectMake(0, _sdcView.bottom+3, KScreenWidth, 40)];
    viewTitle.backgroundColor = [UIColor whiteColor];
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenWidth/2-40, 10, 20, 20)];
    imageView.image = [UIImage imageNamed:@"人气夺宝"];
    UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(imageView.right+5, 10, 100, 20)];
    lable.text = @"人气夺宝";
    [viewTitle addSubview:lable];
    [viewTitle addSubview:imageView];
    [view addSubview:viewTitle];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 1 ;    layout.minimumLineSpacing = 1;    _collectionView = [[RWcollectionView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-64) collectionViewLayout:layout];
    [_collectionView registerClass:[YygCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.backgroundColor =[UIColor colorWithHue:0.00 saturation:0.00 brightness:0.93 alpha:1.00];
    [_collectionView addSubview:view];
    _collectionView.delegate= self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    
    [_collectionView addHeaderWithTarget:self action:@selector(headRefresh)];
    [_collectionView addFooterWithTarget:self action:@selector(footerRefresh)];
}

-(void)headRefresh{
    _page = 1;
    [self requestDataWith:1];
}
-(void)footerRefresh{
    _page ++;
    [self requestDataWith:0];
}

//collectionView 的cell 内容请求
-(void)requestDataWith:(int)first{
     NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setObject:@"10" forKey:@"pageSize"];
    [requestDic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"pageNum"];
     [requestDic setObject:@"1" forKey:@"goodsType"];
    
    [NetWorking NEWjieKouQingQiu:YYG_yygPageList ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
        [TCUtils hiddonLoadingWithView:self.view];
        [_collectionView footerEndRefreshing];
        [_collectionView headerEndRefreshing];
        if (!error) {
            if ([dic[Code] intValue] == 200) {
                if (first == 1) {
                     _allArr = [[NSMutableArray alloc]init];
                    _imageArr = [[NSMutableArray alloc]init];
//                    NSArray * image = dic[Response][@"img"];
//                    [image enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                        [_imageArr addObject:obj[@"imgUrl"]];
//                    }];
                }
                 NSArray * arr = dic[Response][@"resultList"];
               // _sdcView.imageURLStringsGroup = _imageArr;
                [arr enumerateObjectsUsingBlock:^(id  obj, NSUInteger idx, BOOL *  stop) {
                    
                    YYG_AllFirstModel *yiYuanAll = [YYG_AllFirstModel objectFromDictionary:obj];
                    [_allArr addObject:yiYuanAll];
                    yiYuanAll = nil;
                    
                }];
                [_collectionView reloadData];
            }
        }
    }];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    
    return CGSizeMake(KScreenWidth, 175);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return  CGSizeMake(KScreenWidth/2-0.5, 225);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _allArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    _allFirst = _allArr[indexPath.row];
    YygCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:[self requestImageStr]]];
     cell.title.text = _allFirst.name;
     cell.num.text = [NSString stringWithFormat:@"总需:%@",_allFirst.originalPrice];//总共人次
    int shengYu =[_allFirst.originalPrice intValue]-[_allFirst.salesAmount intValue];
    cell.last.text = [NSString stringWithFormat:@"%d",shengYu] ;//剩余人次
     cell.progress.progress =  [_allFirst.salesAmount floatValue] / [_allFirst.originalPrice floatValue];//进度

     cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
//进入详情页
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    YygDetailsViewController * details = [[YygDetailsViewController alloc]init];
    details.firstModel = _allArr[indexPath.row];
    [self.navigationController pushViewController:details animated:YES];
}
//点击轮播图
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{

}


@end
